package listeners;

import playlistData.PlaylistManager;
import utils.DialogCreator;
import xml.NodeUtilities;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.widget.EditText;

import com.unipv14.androidgallery.IListsRefresher;
import com.unipv14.androidgallery.R;

public class RenamePlaylistListener implements OnClickListener {

	private PlaylistManager manager;
	private Activity mainActivity;
	private AlertDialog dialog;
	private IListsRefresher refresher;
	private String selectedfile;
	private DialogCreator dialogCreator;

	public RenamePlaylistListener(Activity mainActivity,
			PlaylistManager manager, IListsRefresher refresher) {
		this.mainActivity = mainActivity;
		this.manager = manager;
		this.refresher = refresher;
		dialogCreator = new DialogCreator();	
	}

	/**
	 * Crea un menu per rinominare la playlist
	 * @param selectedfile
	 */
	public void buttonPressed(String selectedfile) {
		this.selectedfile = selectedfile;
		dialogCreator.createDialogWindow(mainActivity, dialog, R.string.rename_playlist
				, R.layout.add_playlist_layout,this);
	}

	@Override
	public void onClick(DialogInterface dialog, int arg1) {

		EditText textField = (EditText)((AlertDialog)dialog)
				.findViewById(R.id.playlistName);
		String newName = textField.getText().toString();
		if (newName.trim().length() > 0){
		manager.renamePlaylist(this.selectedfile, newName,
				NodeUtilities.defaultPlaylistLocation);
		refresher.refreshList();
		}

	}

}
